package web;


import com.alibaba.fastjson.JSON;
import org.apache.ibatis.session.SqlSession;
import pojo.*;
import service.GoodsService;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.security.Provider;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/goods/*")
public class GoodsServlet extends BaseServlet{
    private GoodsService goodsService = new GoodsService();

    /**
     * 分页条件查询
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 接收 当前页码 和 每页展示条数    url?currentPage=1&pageSize=5
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        // 获取查询条件对象
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串

        //转为 Brand
        Stock stock = JSON.parseObject(params, Stock.class);


        //2. 调用service查询
        PageBean<Stock> pageBean = goodsService.selectByPageAndCondition(currentPage,pageSize,stock);

        //2. 转为JSON
        String jsonString = JSON.toJSONString(pageBean);
        //3. 写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void selectByPageAndConditionB(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 接收 当前页码 和 每页展示条数    url?currentPage=1&pageSize=5
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        // 获取查询条件对象
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串

        //转为 Brand
        Invoice invoice = JSON.parseObject(params, Invoice.class);


        //2. 调用service查询
        PageBean<Invoice> pageBean = goodsService.selectByPageAndConditionB(currentPage,pageSize,invoice);

        //2. 转为JSON
        String jsonString = JSON.toJSONString(pageBean);
        //3. 写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void purchase(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        BufferedReader br = request.getReader();
        String params = br.readLine();
        int id = JSON.parseObject(params, Integer.class);
        List<InogBean> res = goodsService.purchase(id);

        String jsonString = JSON.toJSONString(res);

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);

    }

    public void buys(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        BufferedReader br = request.getReader();
        String params = br.readLine();
        Invoice invoice = JSON.parseObject(params,Invoice.class);

        Date day=new Date();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(day);
        invoice.setCreateTime(time);

        invoice.setFlag(true);

        int nums = invoice.getNum();
        BigDecimal price = invoice.getPrice();
        BigDecimal num = new BigDecimal(0);
        num= BigDecimal.valueOf((int)nums);
        BigDecimal tprice = num.multiply(price);
        invoice.setTprice(tprice);

        System.out.println(time);

        goodsService.inGoods(invoice);
        goodsService.makeInvoice(invoice);

        response.getWriter().write("success");
    }

    public void backb(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        BufferedReader br = request.getReader();
        String params = br.readLine();
        Invoice invoice = JSON.parseObject(params,Invoice.class);

        Date day=new Date();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(day);
        invoice.setCreateTime(time);

        invoice.setFlag(false);

        int nums = invoice.getNum();
        BigDecimal price = invoice.getPrice();
        BigDecimal num = new BigDecimal(0);
        num= BigDecimal.valueOf((int)nums);
        BigDecimal tprice = num.multiply(price);
        invoice.setTprice(tprice);

        System.out.println(time);

        goodsService.outGoods(invoice);
        goodsService.makeOutInvoice(invoice);

        response.getWriter().write("success");
    }


    public void dos(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        BufferedReader br = request.getReader();
        String params = br.readLine();
        StockBean stockBean = JSON.parseObject(params,StockBean.class);
        goodsService.dos(stockBean);
        response.getWriter().write("success");

    }

    public void deleteGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        BufferedReader br = request.getReader();
        String params = br.readLine();
        Integer id = JSON.parseObject(params,Integer.class);
        goodsService.deleteGoods(id);
        response.getWriter().write("success");

    }

    public void alterGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        BufferedReader br = request.getReader();
        String params = br.readLine();
        Goods goods = JSON.parseObject(params,Goods.class);
        goodsService.alterGoods(goods);
        response.getWriter().write("success");

    }

    public void addGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        BufferedReader br = request.getReader();
        String params = br.readLine();
        Goods goods = JSON.parseObject(params,Goods.class);
        goodsService.addGoods(goods);
        response.getWriter().write("success");

    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



            BufferedReader br = request.getReader();
            String params = br.readLine();//json字符串
            Admin admin = JSON.parseObject(params, Admin.class);
            Admin user = goodsService.login(admin.getId(), admin.getPasswd());

            if (user != null) {
                Cookie c_id = new Cookie("id", user.getId());
                Cookie c_name = new Cookie("name", user.getName());
                c_id.setMaxAge(60 * 60 * 24 * 7);
                c_name.setMaxAge(60 * 60 * 24 * 7);
                c_id.setPath("/");
                c_name.setPath("/");
                response.addCookie(c_id);
                response.addCookie(c_name);

                response.getWriter().write("success");

            }
            else {
                System.out.println("false");
            }


    }



}
